using System;
using System.Collections.Generic;
using System.Text;
using LeisureCenter.Api;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace MPlayer
{
    public class MPlayerVideoMedia : BaseMPlayerMedia, IVisualMedia
    {
        public MPlayerVideoMedia(MPlayerPlayer player, string filename)
            : base(player, filename)
        {
        }

        #region private members

        private bool _fullScreen;
        private Rectangle _previewArea = new Rectangle(0, 0, 500, 400);
        private Control _previewControl;
        private int _resolutionWidth;
        private int _resolutionHeight;

        private void PlaceVideoWindow()
        {
            if (!_fullScreen && _resolutionWidth > 0 && _resolutionHeight > 0 && _previewControl != null)
            {
                int lWidth = _resolutionWidth;
                int lHeight = _resolutionHeight;

                double ratio = (double)_previewArea.Height / (double)lHeight;
                ratio = Math.Min((double)_previewArea.Width / (double)lWidth, ratio);

                lWidth = (int)(lWidth * ratio);
                lHeight = (int)(lHeight * ratio);

                SendCommand(MPlayerCommands.SetVideoLeft, _previewArea.Left + (_previewArea.Width - lWidth) / 2);
                SendCommand(MPlayerCommands.SetVideoTop, _previewArea.Top + (_previewArea.Height - lHeight) / 2);
                SendCommand(MPlayerCommands.SetVideoWidth, lWidth);
                SendCommand(MPlayerCommands.SetVideoHeight, lHeight);
            }
        }

        #endregion

        #region protected members

        protected override void OpenFile()
        {
            base.OpenFile();

            SendCommand(MPlayerCommands.GetVideoResolution);
            string resolution = ReadAnswer(MPlayerAnswers.VideoResolution);
            Match match = Regex.Match(resolution, "([0-9]+) *x *([0-9]+)");
            if (match.Success)
            {
                int.TryParse(match.Groups[1].Value, out _resolutionWidth);
                int.TryParse(match.Groups[2].Value, out _resolutionHeight);
            }
        }

        #endregion

        #region public members

        public override MediaKind Kind
        {
            get { return MediaKind.Video; }
        }

        #endregion

        #region IVisualMedia Membres

        public bool FullScreen
        {
            get
            {
                SendCommand(MPlayerCommands.GetFullScreen);
                _fullScreen = ReadIntAnswer(MPlayerAnswers.FullScreen) == 1;
                return _fullScreen;
            }
            set
            {
                if (_fullScreen != value)
                {
                    _fullScreen = value;
                    SendCommand(MPlayerCommands.ToggleFullScreen);
                    if (!_fullScreen)
                    {
                        PlaceVideoWindow();
                    }
                }
            }
        }

        public MediaAspectRatio AspectRatio
        {
            get
            {
                return MediaAspectRatio.Crop;
            }
            set
            {
            }
        }

        public void SetPreviewArea(Control control, Rectangle rect)
        {
            _previewArea = rect;
            _previewControl = control;
            PlaceVideoWindow();
        }

        public void HidePreview()
        {
            _previewArea = new Rectangle((Point)Screen.PrimaryScreen.Bounds.Size, new Size(200, 100));
        }

        #endregion
    }
}
